/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.servlet.niceshot;

import br.com.baia.niceshot.BaiaQueries;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name = "BaiaServlet", urlPatterns = {"/BaiaServlet"})
public class BaiaServlet extends HttpServlet {

    BaiaQueries baiaQueries = new BaiaQueries();

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");

    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("application/json");

        processRequest(request, response);

        try (PrintWriter out = response.getWriter()) {

            String acao = request.getParameter("acao");
            if (acao.equals("remover")) {
                out.println(removeBaia(acao));
            } else {
                out.println(geraBaia(acao));
            }

        }
    }

    private String geraBaia(String acao) {
        return baiaQueries.controleBaias(acao);
    }

    private String removeBaia(String acao) {
        return baiaQueries.controleBaias(acao);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);

    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "BaiaServlet";
    }// </editor-fold>

}
